import {
  getEquipmentData,
  getWisdomScaleData,
  getBusinessType,
  getScaleShow,
  getScaleEdit,
  saveScale,
  repairsScale,
  changePublicScreen,
  savePublicScreen
} from "@/api/equipment";
import * as types from '../../mutation-types'
import { Message } from 'element-ui';
export default {
  namespaced: true, // 封闭
  state: {
    scaleData: [],
    scaleTotal: 0,
    scaleTotalSum: 0
  },
  mutations: {
    [types.SET_SCALE_DATA](state, data) {
      state.scaleData = data;
    },
    [types.SET_SCALE_PAGE](state, total) {
      state.scaleTotal = total;
    },
    [types.SET_SCALE_TOTAL_SUM](state, sum) {
      state.scaleTotalSum = sum;
    }
  },
  getters: {
    filterScaleData(state) {
      return state.scaleData.map(item => {
        return {
          serialId: item.serialId,
          businessName: item.businessName,
          serialNumber: item.serialNumber,
          isOnline: item.isOnline === 1 ? "在线" : "不在线"
        };
      });
    }
  },
  actions: {
    // 获取 公示屏布局 详情
    async changePublicScreen(context, payload) {
      try {
        const { data } = await changePublicScreen();
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    // 提交选择的公示屏布局
    async savePublicScreen(context, payload) {
      const { styleType } = payload
      try {
        const { data } = await savePublicScreen(styleType);
        return data;
      } catch (err) {
        console.log(err);
        Message.error({
          type: 'error',
          message: '设置失败'
        })
      }
    },

    // 获取设备管理数据
    async fetchEquipmentData(context, payload) {
      try {
        const { data } = await getEquipmentData();
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    async fetchWisdomScaleData(context, payload) {
      const { search, type, perPage, page } = payload;
      try {
        const { data } = await getWisdomScaleData(search, type, perPage, page);
        context.commit(types.SET_SCALE_DATA, data.items);
        context.commit(types.SET_SCALE_PAGE, data.total);
        context.commit(types.SET_SCALE_TOTAL_SUM, data.total);
      } catch (err) {
        console.log(err);
        context.commit(types.SET_SCALE_DATA, []);
      }
    },
    // 商户经营种类
    async fetchBusinessType(context, payload) {
      try {
        const { data } = await getBusinessType();
        for (let i = 0; i < data.length; i++) {
          data[i].id = data[i].id + "";
        }
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    // 获取 智慧秤设置详情
    async fetchScaleShow(context, payload) {
      const { serialId } = payload;
      try {
        const { data } = await getScaleShow(serialId);
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    // 提交 智慧秤设置
    async fetchScaleEdit(context, payload) {
      try {
        const { data } = await getScaleEdit(
          payload.serialId,
          payload.intelligentRecognization,
          payload.buttonVoice,
          payload.voiceSing,
          payload.uploadState,
          payload.statisticsShowState,
          payload.setFontDisplay,
          payload.businessType,
          payload.setRoundingOff
        );
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    // 保存新增电子秤
    async fetchSaveScale(context, payload) {
      const { serialNumber, clazz, supplierName } = payload;
      try {
        const { data } = await saveScale(serialNumber, clazz, supplierName);
        return data;
      } catch (err) {
        console.log(err);
      }
    },
    // 报修
    async fetchRepairsScale(context, payload) {
      const { serialNumber, description, photos } = payload;
      try {
        const { data } = await repairsScale(serialNumber, description, photos);
        return data;
      } catch (err) {
        console.log(err);
      }
    }
  }
};
